package com.lecture.view.bean;

import java.sql.Timestamp;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.lecture.hibernate.pojo.Optionsubject;
import com.lecture.hibernate.pojo.School;
import com.lecture.hibernate.pojo.Teacher;
import com.lecture.service.OptionsubjectService;
import com.lecture.view.bean.NavigationResults;
import com.lecture.view.util.MessageFactory;
import com.lecture.hibernate.pojo.Account;

public class OptionSubjectBean extends BaseBean {
	private List<Optionsubject> optionsubjects;
	private DataModel optionsubjectsModel;
	private OptionsubjectService optionsubjectService;
	private Account account;
	private int teacherTag;//0 is not teacher,1 is  teacher;
	public OptionsubjectService getOptionsubjectService()
	{
		return this.optionsubjectService;
	}
	public void setOptionsubjectService(OptionsubjectService service)
	{
		this.optionsubjectService = service;
	}
	public Account getAccount() {
		return this.account;
	}

	public void setAccount(Account logonAccount) {
		this.account = logonAccount;
	}
	public int getTeacherTag()
	{
		return this.teacherTag;
	}
	public void setTeacherTag(int tag)
	{
		this.teacherTag = tag;
	}
	public void setOptionsubjects(List<Optionsubject> optionsubjects) {
		this.optionsubjects = optionsubjects;
	}
	public List<Optionsubject> getOptionsubjects()
	{
		try{
			optionsubjects = optionsubjectService.findOptionsubject();
		return optionsubjects;
		}catch(Exception e)
		{
			return null;
		}
			
	}
	public DataModel getOptionsubjectsModel() {
		if (optionsubjectsModel == null) {
			optionsubjectsModel = new ListDataModel();
			optionsubjectsModel.setWrappedData(getOptionsubjects());
		}
		return optionsubjectsModel;
	}

	public void setOptionsubjectsModel(DataModel optionsubjectsModel) {
		this.optionsubjectsModel = optionsubjectsModel;
	}
	public String findOptionsubject() {
		try{
			System.out.println(this.teacherTag);
			
			if(this.teacherTag==0)
			{
				getOptionsubjectsModel();
			}else
			{
				Teacher teacher = this.account.getTeacher();
				optionsubjects = optionsubjectService.findOSByTeacher(teacher);
				optionsubjectsModel = new ListDataModel();
				optionsubjectsModel.setWrappedData(optionsubjects);
			}
			FacesMessage fmessage = MessageFactory.getMessage(this.context,
			"page.stu_bxtk.success");
			context.addMessage(null, fmessage);
			 return NavigationResults.SUCCESS;
		}catch(Exception e)
		{
			FacesMessage fmessage = MessageFactory.getMessage(this.context,
			"page.stu_bxtk.failure");
			context.addMessage(null, fmessage);
			return NavigationResults.FAILURE;
		}
		
	}
}
